package com.debao.quartz.task;

import com.debao.common.constant.Constants;
import com.debao.common.enums.OrderStatusEnum;
import com.debao.system.domain.CourseCard;
import com.debao.system.domain.CourseCardOrder;
import com.debao.system.service.ICourseCardOrderService;
import com.debao.system.service.ICourseCardService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 统计课程销售量
 */
@Component("courseTask")
public class CourseTask {

    @Resource
    private ICourseCardService courseCardService;
    @Resource
    private ICourseCardOrderService cardOrderService;

    public void censusCourse() {
        List<CourseCard> courseCardList = courseCardService.selectCourseCardList(new CourseCard().setOnline(1l));
        if (courseCardList == null || courseCardList.isEmpty()) {
            return;
        }
        courseCardList.forEach(course -> {
            Long courseId = course.getId();
            //根据课程Id查询相关的支付成功的订单信息
            Integer count = cardOrderService.selectCourseCount(courseId);
            course.setSale(Long.valueOf(count));
            if(count == 0){
                return;
            }
            courseCardService.updateCourseCard(course);
        });
    }
}
